<template>
  <view
    class="flexible_orders_box"
    @touchmove="refresh.moveTouchendHandler"
    @touchstart="refresh.startTouchendHandler"
    @touchend="refresh.endTouchendHandler"
  >
    <!-- 菜单导航栏 -->
    <menu-navigation-bar
      :menu-list="menuList"
      @on-selected="onMenuSelect"
      :is-sticky="true"
    />

    <!-- 内容列表 -->
    <view class="content">
      <flexible-orders
        v-for="(item, index) in dataList"
        :key="index"
        :state="item.ordersState"
        :title="item.ordersMsg"
        :category="item.ordersCategory"
        :create-times="item.createTime.toString()"
        @on-click="toDetail(item)"
      />
    </view>

    <!-- 加载状态 -->
    <load :status="loadStatue" :isImage="dataList.length < 1" />

    <!-- 下拉刷新 -->
    <pull-down-refresh
      :opacity="refresh.moveBottom.value / 3"
      :enble-anim="refresh.isRefresh.value"
    />
  </view>
</template>

<!-- 订单 -->
<script lang="ts" setup>
import flexibleOrders from '@/components/business/orders/flexible-orders.vue'
import { OrdersStateEnum, OrdersTypeEnum } from '@/config/enum/enum'
import menuNavigationBar from '@/components/layout/menu_navigation_bar/menu-navigation-bar.vue'
import { ref, watch } from 'vue'
import { refreshHandler } from '@/components/common/pull_down_refresh/index'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import type {
  PageOrdersByState,
  QueryPageByOrdersByState,
} from '@/config/data/data'
import { ReqSelectOrdersPageByState } from '@/api/orders'

//菜单列表
const menuList = ref<string[]>([])

//菜单选择
const onMenuSelect = (index: number) => {
  queryPage.value.state = menuList.value[index]
  queryPage.value.page = 0
  nextData(true)
}



//防止重复请求
const isReq = ref(true)

//加载状态
const loadStatue = ref(1)

//数据列表
const dataList = ref<PageOrdersByState[]>([])

//下拉刷新控制函数
const refresh = refreshHandler(300)

//跳转详情
const toDetail = (item: PageOrdersByState) => {
  let path = ''
  switch (item.ordersCategory) {
    case OrdersTypeEnum.ORDERS_GOODS:
      path = `/subpkg/bill/detail?id=${item.productsId}&category=${OrdersTypeEnum.ORDERS_GOODS}`
      break
    case OrdersTypeEnum.ORDERS_MAINTENANCE:
      path = `/subpkg/orders/orders_maintenance?id=${item.productsId}&category=${item.ordersCategory}`
      break
  }
  uni.navigateTo({ url: path })
}

//监听下拉刷新
watch(
  () => refresh.isRefresh.value,
  (newValue, oldValue) => {
    if (newValue) {
      nextData(true).finally(() => {
        refresh.refreshEnd(300)
      })
    }
  }
)

//初始化
const initData = () => {
  menuList.value.push(
    ...['全部', OrdersStateEnum.FINISH, OrdersStateEnum.CANCEL]
  )
  nextData()
}

/**
 * 加载下一页数据
 * @param refresh 是否刷新
 */
const nextData = (refresh: boolean = false) => {
  return new Promise((reslove) => {
    if ((!isReq.value || loadStatue.value == -1) && !refresh) {
      reslove(true)
      return
    }
    loadStatue.value = 0
    isReq.value = false
    queryPage.value.page += 1
    ReqSelectOrdersPageByState(queryPage.value)
      .then((res) => {
        if (res.code == 200 && res.data) {
          refresh ? (dataList.value = []) : ''
          dataList.value.push(...res.data)
        }
        loadStatue.value = res.data?.length == queryPage.value.size ? 1 : -1
      })
      .catch(() => {
        loadStatue.value = -2
        setTimeout(() => {
          loadStatue.value = -1
        }, 200)
      })
      .finally(() => {
        isReq.value = true
        reslove(true)
      })
  })
}

//分页参数
const queryPage = ref<QueryPageByOrdersByState>({
  page: 0,
  size: 10,
  state: '',
})

//滑动到底部
onReachBottom(() => {
  nextData()
})

//页面加载
onLoad(() => {
  initData()
})
</script>

<style scoped lang="scss">
.flexible_orders_box {
  width: 100%;
  .content {
    padding: 0 30rpx;
    width: calc(100% - 60rpx);
    background-color: $uni-bg-color-grey;
  }
}
</style>
